﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Collections.Generic;
using CMN.Common;
using CMN.Entity;
using CMN.Models;
using CMN.Models.Interfaces;
using System.Text;
namespace CMN.Domain
{
    public class AsyncUpdateUrlLog : AsyncBase
    {

        public string StrWhere = string.Empty;
        public AsyncUpdateUrlLog(ISynchronizeInvoke target)
            : base(target)
        {
           
        }
        //根据任务ID执行任务
        protected override void AsyncWork()
        {
            Update();                    
                
        }

        //已知任务步骤,任务可以取消
        public  void Update()
        {

            IDataService<DomainsEntity> urlSv = ServiceBuilder.BuildDomainsService();
            QueryParam qp = new QueryParam();
            qp.Where = StrWhere;
            int recordNum=0;
            List<DomainsEntity> urlList = urlSv.List(qp, out recordNum);

            this.m_action = urlList.Count;
            bool is_cancel = false;
            string strContent="";
             string index="";
            for (int i = 0; i < urlList.Count; ++i)
            {
                //调用基类的方法检查任务是否取消
                is_cancel = isCnacel();
                if (is_cancel)
                    break;
                
                try
                {
                    if (urlList[i].SiteUrl.ToLower().IndexOf("http") == -1)
                    {
                        urlList[i].SiteUrl = string.Concat("http://", urlList[i].SiteUrl);
                    }
                    Uri baseUri = new Uri(urlList[i].SiteUrl);

                    strContent = Utils.GetRequestString("http://www.baidu.com/s?ie=UTF-8&wd=site%3A" + baseUri.Authority, 16000, 1, Encoding.GetEncoding("utf-8"), null);
                     index = Utils.GetCutString(strContent, "该网站共有", "个网页被百度收录", false);
                    if (index == "")
                        index = Utils.GetCutString(strContent, "找到相关结果数约", "个", false);
                    if (index == "" && strContent.IndexOf("很抱歉") > -1)
                    {
                        index = "0";
                    }
                    urlList[i].IndexNum = index.Replace(",", "");
                    //外链

                    strContent = Utils.GetRequestString("http://www.baidu.com/s?ie=UTF-8&wd=\"www." + baseUri.Authority + "\"", 16000, 1, Encoding.GetEncoding("utf-8"), null);
                    index = Utils.GetCutString(strContent, "百度为您找到相关结果约", "个", false);
                    if (index == "")
                        index = Utils.GetCutString(strContent, "找到相关结果数约", "个", false);
                    if (index == "" && strContent.IndexOf("很抱歉") > -1)
                    {
                        index = "0";
                    }
                    urlList[i].OutUrlNum = index.Replace(",", "");
                    //pr
                    strContent = Utils.GetRequestString("http://seo.chinaz.com/?host=" + baseUri.Authority, 16000, 1, Encoding.GetEncoding("utf-8"), null);
                    string strPR = Utils.GetCutString(strContent, "/template/default/images/ranks/Rank_", ".gif", false);
                    if (strPR.Trim() == "-2")
                        strPR = "0";
                    urlList[i].PR = Utils.StringToInt(strPR, 0);
                    string strBR = Utils.GetCutString(strContent, "/template/default/images/baiduapp/", ".gif", false);
                    urlList[i].BR = Utils.StringToInt(strBR, 0);
                    urlSv.Update(urlList[i]);
                }
                catch (Exception ex)
                {
                    AppLog.Write(ex.Message, AppLog.LogMessageType.Error);
                }
                OnTaskProgress(this, i);
                System.Threading.Thread.Sleep(100);
            }
            if (!is_cancel)
                OnTaskDone(this, null);
            this.Stop();
           
        }

    }
}
